package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.MessageConstant;
import com.itheima.dao.BlogDao;
import com.itheima.dao.BlogMapper;
import com.itheima.dao.BlogSortMapper;
import com.itheima.dao.TagMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogSort;
import com.itheima.pojo.Tag;
import com.itheima.service.BlogService;
import com.itheima.service.AdminTagService;
import com.itheima.service.TagService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.ResultUtil;
import com.itheima.utils.StringUtils;
import com.itheima.vo.BlogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private BlogDao blogDao;

    @Autowired
    private AdminTagService adminTagService;

    @Autowired
    private BlogSortMapper blogSortMapper;

    @Autowired
    private TagService tagService;

    @Autowired
    private TagMapper tagMapper;

    /**
     * 添加博客
     *
     * @param blogVo
     */
    @Override
    public void add(BlogVo blogVo) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogVo, blog);
        blog.setUid(blogVo.getUid());
        blog.setStatus((byte) 1);
        blogMapper.insertSelective(blog);

        BlogSort blogSort = new BlogSort();
        BeanUtils.copyProperties(blogVo.getBlogSort(), blogSort);
        blogSort.setUid(blogVo.getUid());
        blogSort.setStatus(1);
        blogSortMapper.insertSelective(blogSort);

        for (Tag tag : blogVo.getTagList()) {
            tag.setUid(blogVo.getUid());
            tag.setStatus( 1);
            tagMapper.insertSelective(tag);
        }
    }

    @Override
    public List<Blog> getBlogByLevel(Integer level , Integer currentPage , Integer useSort) {
        if (currentPage ==null || currentPage <1){
            currentPage = 1;
        }
        PageHelper.startPage(currentPage,10);
        //构建整体条件对象，条件中对象构造方法中参数需要建立和表的映射关系select * from `tb_brand`
        Example example=new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("level",level);
        if (useSort == 0){
            //如果userSort是0就是升序 否则就是降序
            example.orderBy("sort").asc();
        }
        //根据权重排序
        example.orderBy("sort").desc();
        List<Blog> list = blogDao.selectByExample(example);
        return list;
    }
    /**
     * 删除博客
     *
     * @param blogVo
     */
    @Override
    public void delete(BlogVo blogVo) {
       /* Example example = new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid", blogVo.getUid());
        blogMapper.deleteByExample(example);

        Example example2 = new Example(BlogSort.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("uid", blogVo.getUid());
        blogSortMapper.deleteByExample(example2);

        for (Tag tag : blogVo.getTagList()) {
            tag.setUid(blogVo.getUid());
            tagMapper.deleteByPrimaryKey(tag);

        }*/

        Blog blog = new Blog();
        BeanUtils.copyProperties(blogVo, blog);
        blog.setUid(blogVo.getUid());
        blog.setStatus((byte) 0);
        blogMapper.updateByPrimaryKeySelective(blog);

        BlogSort blogSort = new BlogSort();
        BeanUtils.copyProperties(blogVo.getBlogSort(), blogSort);
        blogSort.setUid(blogVo.getUid());
        blogSort.setStatus(0);
        blogSortMapper.updateByPrimaryKeySelective(blogSort);

        for (Tag tag : blogVo.getTagList()) {
            tag.setUid(blogVo.getUid());
            tag.setStatus(0);
            tagMapper.updateByPrimaryKeySelective(tag);
        }
    }

    /**
     * 批量删除选中博客
     *
     * @param blogVoList
     */
    @Override
    public void deleteBatch(List<BlogVo> blogVoList) {
        //判断传过来的集合是否为空
        if (blogVoList == null) {
            return;
        }

        //不为空就遍历删除
        for (BlogVo blogVo : blogVoList) {
            this.delete(blogVo);
        }
    }

    /**
     * 编辑博客
     *
     * @param blogVo
     */
    @Override
    public void edit(BlogVo blogVo) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogVo, blog);
        blog.setUid(blogVo.getUid());
        blogMapper.updateByPrimaryKeySelective(blog);

        BlogSort blogSort = new BlogSort();
        BeanUtils.copyProperties(blogVo.getBlogSort(), blogSort);
        blogSort.setUid(blogVo.getUid());
        blogSortMapper.updateByPrimaryKeySelective(blogSort);

        for (Tag tag : blogVo.getTagList()) {
            tag.setUid(blogVo.getUid());
            tagMapper.updateByPrimaryKeySelective(tag);
        }
    }


    /**
     * 获取博客列表，根据条件进行分页查询
     *
     * @param keyword
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findAll(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Map> map = blogMapper.findAllByKeyword(keyword);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(map);
        pageResult.setTotal((long) map.size());
        pageResult.setCurrent(currentPage);
        pageResult.setSize(pageSize);
        return pageResult;
    }

    //通过tagUid获取文章
    @Override
    public List<Blog> getArticleByTagUid(String tagUid, Long currentPage, Long pageSize) {
        if (currentPage==null||currentPage==0){
            currentPage=1L;
        }
        if (pageSize==null||pageSize==0){
            pageSize=10L;
        }
        PageHelper.startPage(currentPage.intValue(),pageSize.intValue());


        List<Blog> blogList = blogDao.selectByTagUid(tagUid);
        return blogList;
    }

    //通过月份获取文章
    @Override
    public List<Blog> getArticleByMonth(String monthDate) {
        String momth = monthDate.substring(5, 7);
        String beginDate="2020-"+momth+"-01";
        String endDate="2020-"+momth+"-31";
        return blogDao.findBlogByMonth(beginDate,endDate);
    }

    //根据月份归档
    @Override
    public List<String> getSortList() {
        //获取当前月
        String month = DateUtils.getMonth().toString();
        //获取当前年
        String year= DateUtils.getYears().toString();
        List<String> months = new ArrayList<>();
        String begin=year+"年01月";
        String end = year +"年"+month+"月";
        Date d1 = null;//定义起bai始日期
        Date d2 = null;//定义结du束日期
        try {
            d1 = new SimpleDateFormat("yyyy年MM月").parse(begin);
            d2 = new SimpleDateFormat("yyyy年MM月").parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar dd = Calendar.getInstance();//定义日期实例
        dd.setTime(d1);//设置日期起始时间
        while(dd.getTime().before(d2)) {//判断是否到结束日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            String str = sdf.format(dd.getTime());
            System.out.println(str);//输出日期结果
            dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
            months.add(str);
        }
        return months;
    }
    @Override
    public Blog getBlogByUid(String uid) {
        Blog blog = blogDao.selectByPrimaryKey(uid);
        return blog;
    }

    @Override
    public Integer getBlogPraiseCountByUid(String uid) {
        Blog blog = blogDao.selectByPrimaryKey(uid);
        if (blog==null){
            return null;
        }
        Integer clickCount = blog.getClickCount();
        return clickCount;
    }

    @Override
    public List<Blog> getSameBlogByBlogUid(String blogUid) {
        Blog blog = blogDao.selectByPrimaryKey(blogUid);
        if (blog==null){
            return null;
        }
        String blogSortUid = blog.getBlogSortUid();
        if (StringUtils.isEmpty(blogSortUid)){
            return null;
        }
        Example example = new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",1);
        criteria.andEqualTo("blogSortUid",blogSortUid);
        List<Blog> blogList = blogDao.selectByExample(example);
        return blogList;
    }

    @Override
    public Page<Blog> getSameBlogByTagUid(String tagUid, Long currentPage, Long pageSize) {

        Tag tag = tagService.getTagByUid(tagUid);
        if (tag==null){
            return null;
        }

        PageHelper.startPage(currentPage.intValue(),pageSize.intValue());
        Example example = new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",1);
        criteria.andEqualTo("tagUid",tagUid);
        Page<Blog> blogList = (Page<Blog>)blogDao.selectByExample(example);
        return blogList;
    }


    @Override
    public String praiseBlogByUid(String uid) {
        Blog blog = blogDao.selectByPrimaryKey(uid);
        if (blog==null){
            return ResultUtil.result("401", MessageConstant.BLOG_NULL_FAIL);
        }
        Integer clickCount = blog.getClickCount();
        blog.setClickCount(clickCount+1);
        blogDao.updateByPrimaryKeySelective(blog);
        return ResultUtil.result("200",MessageConstant.BLOG_CLICKCOUNT_SUCCESS);
    }




}
